# file:        Makefile
# author:      Andrea Vedaldi
# description: Build SIFT mex files

NAME   := vlutil
VER    := 1.1.8
DIST    = $(NAME)-$(VER)
BINDIST = $(DIST)-$(ARCH)


# --------------------------------------------------------------------
#                                                       Error messages
# --------------------------------------------------------------------

err_no_arch  =
err_no_arch +=$(shell echo "** Unknown host architecture '$(UNAME)'. This identifier"   1>&2)
err_no_arch +=$(shell echo "** was obtained by running 'uname -sp'. Edit the Makefile " 1>&2)
err_no_arch +=$(shell echo "** to add the appropriate configuration."                   1>&2)
err_no_arch +=Configuration failed

# --------------------------------------------------------------------
#                                                        Configuration
# --------------------------------------------------------------------

# Determine on the flight the system we are running on
# generic flags

CFLAGS      += -O0 -I. -pedantic -Wall -std=c99 -g
LDFLAGS     +=
MEX_CFLAGS  = CFLAGS='$$CFLAGS $(CFLAGS)'

# Determine on the flight the system we are running on
Darwin_PPC_ARCH     := mac
Darwin_powerpc_ARCH := mac
Darwin_i386_ARCH    := mci
Linux_i386_ARCH     := glx
Linux_unknown_ARCH  := glx

UNAME               := $(shell uname -sp)
ARCH                := $($(shell echo "$(UNAME)" | tr \  _)_ARCH)

ifeq ($(ARCH),)
die:=$(error $(err_no_arch))
endif

mac_CFLAGS       :=
mac_LDFLAGS      := -DBIG_ENDIAN
mac_MEX_CFLAGS   :=
mac_MEX_SUFFIX   := mexmac

mci_CFLAGS       := -DLITTLE_ENDIAN
mci_LDFLAGS      :=
mci_MEX_CFLAGS   :=
mci_MEX_SUFFIX   := mexmaci

glx_BINDIR       := bin/glx
glx_CFLAGS       := -DLITTLE_ENDIAN
glx_LDFLAGS      := -lm
glx_MEX_CFLAGS   :=
glx_MEX_SUFFIX   := mexglx

CFLAGS           += $($(ARCH)_CFLAGS)
LDFLAGS          += $($(ARCH)_LDFLAGS)
MEX_SUFFIX       += $($(ARCH)_MEX_SUFFIX)
MEX_CFLAGS       += $($(ARCH)_MEX_CFLAGS)

# --------------------------------------------------------------------
#
# --------------------------------------------------------------------

vpath %.mex.c   toolbox
vpath %.mex.cpp toolbox

src  := $(wildcard toolbox/*.mex.c) $(wildcard toolbox/*.mex.cpp)
msrc := $(wildcard toolbox/*.m)
stem := $(notdir $(basename $(basename $(src))))
tgt  := $(addprefix mex/, $(addsuffix .$(MEX_SUFFIX),$(stem)))

mex/%.$(MEX_SUFFIX) : %.mex.c
	mex -I. $(MEX_CFLAGS) $< -outdir 'mex'
	@mv mex/$*.mex.$(MEX_SUFFIX) mex/$*.$(MEX_SUFFIX)

mex/%.$(MEX_SUFFIX) : %.mex.cpp
	mex -I. $(MEX_CFLAGS) $< -outdir 'mex'
	@mv mex/$*.mex.$(MEX_SUFFIX) mex/$*.$(MEX_SUFFIX)

.PHONY: all
all: $(tgt)

.PHONY: info
info :
	@echo src  = $(src)
	@echo stem = $(stem)
	@echo tgt  = $(tgt)
	@echo "UNAME       = $(UNAME)"
	@echo "ARCH        = $(ARCH)"
	@echo "CFLAGS      = $(CFLAGS)"
	@echo "LDFLAGS     = $(LDFLAGS)"
	@echo "MEX_CFLAGS  = $(MEX_CFLAGS)"
	@echo "MEX_SUFFIX  = $(MEX_SUFFIX)"

# PDF documentation
.PHONY: doc
doc: vlutil.html

vlutil.html : $(msrc)
	mdoc --output=vlutil.html --exclude 'test_*' toolbox

.PHONY: clean
clean:
	rm -f $(tgt)
	rm -f .DS_Store .gdb_history
	find . -name '*~' -exec rm -f \{\} \;

.PHONY: distclean
distclean: clean
	rm -f mex/*.mexmac mex/*.mexglx
	rm -f vlutil.html
	rm -rf $(NAME)-*

.PHONY: dist
dist: distclean
	echo Version $(VER) >TIMESTAMP
	echo Archive created on `date` >>TIMESTAMP
	tar chzvf $(DIST).tar.gz ../$(notdir $(CURDIR))

.PHONY: bindist
bindist: all doc
	test -e $(BINDIST) || mkdir $(BINDIST)
	cp README toolbox/*.m vlutil.html mex/*.$(MEX_SUFFIX) $(BINDIST)
	cd $(BINDIST) ; strip -S *.$(MEX_SUFFIX) $(notdir $(LIB))	
	echo Version $(VER) >$(BINDIST)/TIMESTAMP
	echo Package created on `date` >>$(BINDIST)/TIMESTAMP
	tar cvzf $(BINDIST).tar.gz $(BINDIST)

.PHONY: autorights
autorights: distclean
	autorights \
	  . \
	  --recursive \
	  --template gpl \
	  --years 2006 \
	  --authors "Andrea Vedaldi" \
	  --holders "Andrea Vedaldi" \
	  --program "VLUtil"
